package view
{
	
	import event.GotoClickButtonEvent;
	import event.SendMessageEvent;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.events.FlexEvent;
	
	import spark.components.Button;
	import spark.components.List;
	import spark.components.TextInput;
	import spark.components.supportClasses.SkinnableComponent;
	
	[Event(name="gotoClickButton", type="event.GotoClickButtonEvent")]
	[Event(name="sendMessage", type="event.SendMessageEvent")]
	public class ChatRoBot extends SkinnableComponent
	{
		
		[SkinPart] public var mainTextInput:TextInput;
		[SkinPart] public var conversationList:List;
		[SkinPart] public var historyButton:Button;
		
		[Bindable] public var messages:ArrayCollection;
		
		override protected function getCurrentSkinState():String
		{
			return super.getCurrentSkinState();
		} 
		
		override protected function partAdded(partName:String, instance:Object) : void
		{
			super.partAdded(partName, instance);
			
			if (instance == mainTextInput)
			{
				mainTextInput.addEventListener(FlexEvent.ENTER, handleSendMessage);
			}
			if (instance == historyButton)
			{
				historyButton.addEventListener(MouseEvent.CLICK, handleHistoryButtonClick);
			}
		}
		
		public function handleHistoryButtonClick(e:MouseEvent):void
		{
			dispatchEvent(new GotoClickButtonEvent(GotoClickButtonEvent.GOTOCLICKBUTTONEVENT));
		}
		
		public function handleSendMessage(e:FlexEvent):void
		{
			dispatchEvent(new SendMessageEvent(SendMessageEvent.SENDMESSAGE, mainTextInput.text.toString()));
			mainTextInput.text = '';
		}
		
		override protected function partRemoved(partName:String, instance:Object) : void
		{
			super.partRemoved(partName, instance);
		}
		
	}
}